home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #1 / Ham Radio 2000.iso / ham2000 / hf / dsp / source / cpfsk.asm < prev    next >
Encoding:
Assembly Source File  |  1991-08-31  |  9.9 KB  |  421 lines

  1.     page    132,63,1,1
  2.     opt    rc
  3.     title    'CPFSK Modulation/Demodulation'
  4.  
  5. ;***************************************************************
  6. ;* CPFSK.ASM -- CPFSK Modulation/Demodulation               *
  7. ;*                                   *
  8. ;* Provides interrupt based SSI handling, sample buffering and *
  9. ;* CPFSK modulation and demodulation.                   *
  10. ;*                                   *
  11. ;* CPFSK demodulation is based on article               *
  12. ;*    Osborne, W., P., Lunz, M., B.:                   *
  13. ;*    "Coherent and Noncoherent Detection of CPFSK",           *
  14. ;*    IEEE Trans. on Comm., Vol. 22, No. 8, August 1974      *
  15. ;*                                   *
  16. ;* Symbol syncronization is based on article               *
  17. ;*    Glisic, S., G.:                        *
  18. ;*    "Symbol Syncronization in Digital Communications       *
  19. ;*     Using Partial Response CPM signaling",                *
  20. ;*    IEEE Trans. on Comm., Vol. 37, No. 3, March 1989       *
  21. ;*                                   *
  22. ;* This module uses registers as follows:               *
  23. ;*  r0 - general purpose (temporary use)               *
  24. ;*  r2 - input sample write pointer                   *
  25. ;*  r3 - output sample read pointer                   *
  26. ;*  r6 - input sample read pointer                   *
  27. ;*  r7 - output sample write pointer                   *
  28. ;*                                   *
  29. ;* Copyright (C) 1990, 1991 by Alef Null. All rights reserved. *
  30. ;* Author(s): Jarkko Vuori, OH2LNS                   *
  31. ;* Modification(s):                           *
  32. ;***************************************************************
  33.  
  34.     nolist
  35.     include 'ioequlc'
  36.     list
  37.  
  38.     section CPFSK
  39.     xdef    ssi_ini,modem_i
  40.     xdef    m_loop
  41.     xref    putbit,getbit
  42.     xref    m_cra,m_crb,m_tde,m_sr,m_tx
  43.  
  44.     org    p:
  45.  
  46.     nolist
  47.     include 'macros'
  48.     list
  49.  
  50. ; CPFSK sync parameters
  51. cM0    equ     4                    ; random walk filter constant
  52. adjlen    equ    32                    ; ADC timing control lenght (in samples)
  53. analen    equ    18                    ; sample quality analyzing lenght
  54. trshold equ    16                    ; sample quality decision treshold
  55.  
  56. ; complex correlation (result in a1)
  57. ccorr    macro
  58. ; s0 real -> b (real mode)
  59.     conv    0,N,r6,r0,a
  60.     move    a1,x0                    ; square result
  61.     mpy    x0,x0,a     (r6)+n6
  62.  
  63. ; s0 imaginary -> a (real mode)
  64.     conv    0,N,r6,r0,b
  65.     move    b1,x0                    ; square result
  66.     macr    x0,x0,a     (r6)+n6
  67.     endm
  68.  
  69.  
  70. ;****************************
  71. ;*    SSI initialization    *
  72. ;****************************
  73. ssi_ini
  74. ; initialize SSI,
  75.     IF    !DEBUG
  76.     movep            #$4000,x:m_cra        ; 16 bit word
  77.     movep            #$f200,x:m_crb        ; syncronous,word frame,ext clk
  78.     ELSE
  79.     movep            #$4011,x:m_cra        ; 16 bit word, abt. 19.2 ksamples/s
  80.     movep            #$f620,x:m_crb        ; syncronous,word frame,ext clk
  81.     ENDIF
  82.  
  83.     rts
  84.  
  85.  
  86. ;****************************
  87. ;*      ADC & MODEM        *
  88. ;*    initialization        *
  89. ;****************************
  90. modem_i
  91. ; program the A/D & D/A converter chip (TLC32044, master clock is 5.184MHz)
  92. ; enable A/D high-pass filter, disable loopback, disable AUX IN,
  93. ; select syncronous mode, select input span 3Vpp, insert sinx/x correction
  94. ; lowpass cutoff frequency at    3420Hz -> TA  = 10 (SCF = 259kHz)
  95. ; sampling frequency is     7200Hz -> TB  = 36
  96. ; unit sampling correction time  1.2uS -> TA' =  6
  97.     pgmtlc    %10101001,10,6,36
  98.  
  99. ; sample handling,
  100.     move            #<insamp,r6
  101.     move            #<inN-N,n6
  102.     move            #<inN-1,m6
  103.     move            r6,r2
  104.     move            m6,m2
  105.     movi            <costab,x:<frqptr
  106.     movi            <f1,x:<frq
  107.     movi    <m,x:<rsamc
  108.     movi    0,x:<samque
  109.  
  110.     move            #<outsamp,r3
  111.     move            #<outN-1,m3
  112.     move            #<outsamp+1,r7
  113.     move            m3,m7
  114.     movi    <1,x:<xsamc
  115.  
  116. ; symbol syncro logic,
  117.     movi    cM0,x:<rwalk
  118.     movi    analen,x:<anacnt
  119.     movi    0,x:<goodcnt
  120.  
  121. ; ADC command word
  122.     movi    0,x:<adcnt
  123.  
  124.     rts
  125.  
  126.  
  127. ;****************************
  128. ;*      Main Loop        *
  129. ;****************************
  130. loop    wait
  131. m_loop
  132.  
  133.  
  134. ;****************************
  135. ;*      Receiver        *
  136. ;****************************
  137. ; check if there are new samples received
  138. rec    move            r2,a
  139.     move            r6,x0
  140.     cmp    x0,a        #<refs,r0
  141.     jeq    <xmit
  142.  
  143. ; yes, there indeed are new samples, check if receiver enabled
  144.     jset    #ptt,x:<status,xmit
  145.  
  146. ; yes, correlate with s0 signal
  147.     ccorr
  148.     move    a1,x1
  149.  
  150. ; correlate with s1 signal
  151.     ccorr
  152.  
  153. ; calculate result, limit to binary and queue received bits
  154.     sub    x1,a        x:<samque,b1
  155.     rol    b        (r6)-
  156.     move            b1,x:<samque
  157.  
  158. ; check if one symbol (m samples) received
  159.     move            #>1,x0
  160.     move            #>-1,x1
  161.     move            x:<rsamc,b
  162.     sub    x0,b        #>m,y0
  163.     move            b1,x:<rsamc
  164.     jne    <ssi_end
  165.     move            y0,x:<rsamc
  166.  
  167. ; yes, analyze eye pattern (= symbol quality)
  168. ; (by checking that samples nearby decision sample are equal)
  169. ; (...|X****X|X****X|X**...)
  170. ;   where X ignored samples
  171. ;      * analyzed sample
  172. ;      | symbol boundaries
  173.     clr    b        #>%011110000,y0
  174.     move            x:<samque,b1
  175.     and    y0,b
  176.     cmp    y0,b        #>%000000000,y0
  177.     jeq    <ssi_r0a
  178.     cmp    y0,b
  179.     jne    <ssi_r0b
  180.  
  181. ssi_r0a move            x:<goodcnt,b        ; good quality symbol
  182.     add    x0,b
  183.     move            b1,x:<goodcnt
  184.  
  185. ssi_r0b move            x:<anacnt,b         ; update analyses counter
  186.     sub    x0,b        #>analen,y0
  187.     move            b1,x:<anacnt
  188.     jne    <ssi_rs
  189.     move            y0,x:<anacnt
  190.  
  191.     move            x:<goodcnt,b        ; make dcd decision
  192.     move            #>trshold,y0
  193.     cmp    y0,b
  194.     jhs    <ssi_r0c
  195.     bclr    #dcd,x:<status                ; dcd off
  196.     jcc    <ssi_r0d
  197.     dcdoff
  198.     jsset    #req,x:<status,kickx            ; call persistence routine if needed
  199.     jmp    <ssi_r0d
  200. ssi_r0c bset    #dcd,x:<status                ; dcd on
  201.     jcs    <ssi_r0d
  202.     dcdon
  203.  
  204. ssi_r0d clr    b
  205.     move            b,x:<goodcnt
  206.  
  207. ; check if there are transitions (by analyzing the sample between decision samples)
  208. ; (...|00X00*|00X00*|00X...)
  209. ;   where X decision sample
  210. ;      * analyzed sample
  211. ;      | symbol boundaries
  212. ssi_rs    clr    a        #>1,x0
  213.     move            #>-1,x1
  214.     jclr    #0,x:<samque,ssi_l1
  215.     jset    #6,x:<samque,ssi_le
  216. ; 1x0
  217.     move            x0,a
  218.     btst    #3,x:<samque
  219.     tcc    x1,a
  220.     jmp    <ssi_le
  221. ssi_l1    jclr    #6,x:<samque,ssi_le
  222. ; 0x1
  223.     move            x1,a
  224.     btst    #3,x:<samque
  225.     tcc    x0,a
  226.  
  227. ; symbol syncro
  228. ssi_le    tst    a        x:<rwalk,x0
  229.     jeq    <ssi_r4
  230.     add    x0,a        #>2*cM0,x0
  231.     jpl    <ssi_r2
  232. ; retard sampling
  233.     move            #cM0,a1
  234.     movib            retard,x:<adccmd
  235.     movib            adjlen,x:<adcnt
  236.     jmp    <ssi_r3
  237. ;
  238. ssi_r2    cmp    x0,a
  239.     jle    <ssi_r3
  240. ; advance sampling
  241.     move            #cM0,a1
  242.     movib            advance,x:<adccmd
  243.     movib            adjlen,x:<adcnt
  244. ;
  245. ssi_r3    move            a1,x:<rwalk
  246.  
  247. ; make symbol decision (with NRZ-S decoding)
  248. ssi_r4    move            x:<prvrsym,x0
  249.     move            x:<samque,a1
  250.     eor    x0,a        a1,x:<prvrsym
  251.     not    a
  252.     move            a1,x:<tmp
  253.     btst    #6,x:<tmp
  254. ; forward to the HDLC handler
  255.     jsr    <putbit
  256.  
  257. ssi_end jmp    <rec
  258.  
  259.  
  260. ;****************************
  261. ;*     Transmitter        *
  262. ;****************************
  263. ; check if we can calculate new samples
  264. xmit    move            r3,a
  265.     move            r7,x0
  266.     cmp    x0,a
  267.     jeq    <loop
  268.  
  269. ; yes, check if the transmitter is on at all
  270.     clr    a        #>1,x0
  271.     jclr    #ptt,x:<status,xmt2
  272.  
  273. ; xmit mode, check if we have sent a whole symbol
  274.     move            x:<xsamc,a
  275.     sub    x0,a        #>m,x0
  276.     move            a,x:<xsamc
  277.     jne    <xmt1
  278.     move            x0,x:<xsamc
  279.  
  280. ; yes, check on what phase we are
  281.     jset    #xmt,x:status,xmt0
  282.  
  283. ; training phase, send continuous preamble (zero pattern)
  284.     andi    #$fe,ccr
  285.     jmp    <morebit
  286.  
  287. ; data phase, get a new bit to xmit
  288. xmt0    jsr    <getbit
  289.     jne    <morebit
  290. ; there is no more bits to send, switch to receiving mode
  291.     jsr    <shutx
  292.     andi    #$fc,mr
  293.  
  294. ; NRZ-S coding
  295. morebit rol    a        x:<prvxsym,x0
  296.     not    a
  297.     eor    x0,a        #>f1,x0
  298.     ror    a        a1,x:<prvxsym
  299.  
  300. ; determine frequency
  301.     move            #<f0,b1
  302.     tcs    x0,b
  303.     move            b1,x:<frq
  304.  
  305. ; send a new bit
  306. xmt1    move            x:<frqptr,r0
  307.     move            x:<frq,n0
  308.     move            #<ent-1,m0
  309.     move            x:<gain,x1
  310.     move            x:(r0)+n0,x0        ; adjust output level
  311.     move            r0,x:<frqptr
  312.     move            #-1,m0
  313.     mpyr    x0,x1,a     #>$fffc<<8,x0
  314. xmt2    and    x0,a        x:<adcnt,b
  315.     tst    b        #>1,y0            ; check if we must adjust timing
  316.     jeq    <xmt3
  317.     sub    y0,b        x:<adccmd,x0        ; yes, add converter chip control cmds
  318.     or    x0,a        b1,x:<adcnt
  319. xmt3    move            a,x:(r7)+
  320.  
  321. scx_end jmp    <xmit
  322.  
  323.  
  324.  
  325. ;****************************
  326. ;*     DATA - AREA        *
  327. ;****************************
  328.  
  329.     org    x:
  330.  
  331. adccmd    ds    1                    ; ADC timing command
  332. adcnt    ds    1                    ; ADC timing command counter
  333. rsamc    ds    1                    ; receiver sample counter
  334. xsamc    ds    1                    ; xmitter sample counter
  335. samque    ds    1                    ; received sample queue
  336. prvrsym ds    1                    ; previous received symbol
  337. prvxsym ds    1                    ; previous xmitted symbol
  338. rwalk    ds    1                    ; random walk filter counter
  339. anacnt    ds    1                    ; signal quality analyzing lenght counter
  340. goodcnt ds    1                    ; good symbol counter
  341. frqptr    ds    1
  342. frq    ds    1
  343.  
  344. tmp    ds    1
  345.  
  346.     org    y:
  347.  
  348. ; Complex reference signals
  349. ; m=6, bits=3
  350. refs
  351.  
  352. ; s0
  353. ; real
  354.     dc  -0.02856969,-0.00150768,-0.01250000,-0.01099232
  355.     dc  0.04106969,0.02500000,-0.04924039,0.00868241
  356.     dc  0.04330127,-0.03830222,-0.01710101,0.05000000
  357.     dc  0.00394950,-0.03165111,-0.00334936,-0.00815880
  358.     dc  -0.01212019,0.03750000
  359. ; imaginary
  360.     dc  -0.00249952,0.00855050,-0.00334936,0.03020114
  361.     dc  0.01915111,-0.04330127,-0.00868241,0.04924039
  362.     dc  -0.02500000,-0.03213938,0.04698463,0.00000000
  363.     dc  -0.04514295,-0.00558094,0.01250000,-0.00296956
  364.     dc  0.02599184,0.02165064
  365.  
  366. ; s1
  367. ; real
  368.     dc  -0.01212019,-0.00815880,-0.00334936,-0.03165111
  369.     dc  0.00394950,0.05000000,0.02500000,-0.02500000
  370.     dc  -0.05000000,-0.02500000,0.02500000,0.05000000
  371.     dc  0.00394950,-0.03165111,-0.00334936,-0.00815880
  372.     dc  -0.01212019,0.03750000
  373. ; imaginary
  374.     dc  -0.02599184,0.00296956,-0.01250000,0.00558094
  375.     dc  0.04514295,-0.00000000,-0.04330127,-0.04330127
  376.     dc  0.00000000,0.04330127,0.04330127,0.00000000
  377.     dc  -0.04514295,-0.00558094,0.01250000,-0.00296956
  378.     dc  0.02599184,0.02165064
  379.  
  380.     endsec
  381.  
  382.  
  383. ;****************************
  384. ;*    M*BITS MODULO DATA    *
  385. ;****************************
  386.     section CPFSKData
  387.     xdef    N,m,inN,outN
  388.     xdef    insamp,outsamp
  389.     xdef    ent,f0,f1,costab
  390.  
  391.     org    x:
  392.  
  393. ; CPFSK demod parameters
  394. m    equ    6                    ; samples per symbol
  395. bits    equ    3                    ; number of observed bits
  396. N    equ    m*bits
  397. inN    equ    32
  398. outN    equ    8
  399.  
  400. insamp    ds    inN
  401. outsamp ds    outN
  402.     ds    32-outN                 ; for modulo justification
  403.  
  404. pi    equ    3.141592654
  405. ent    equ    36
  406. f0    equ    11
  407. f1    equ    6
  408. freq    equ    2.0*pi/@cvf(ent)
  409.  
  410. ; cosine lookuptable
  411. costab
  412. count    set    0
  413.     dup    ent
  414.     dc    0.999999*@cos(@cvf(count)*freq)
  415. count    set    count+1
  416.     endm
  417.  
  418.     endsec
  419.  
  420.     end
  421.